<?php
namespace App\Models;

/**
 * 系统内容分类对象
 */
class MNews extends BaseModel
{
    public $table = "news";

    //表关系区===========================================================================

    /**
     * 父类别
     */
    public function classify()
    {
        return $this->belongsTo(MClassify::class, "cid", "id");
    }

    //数据验证区===========================================================================
    /**
     * 信息存储验证
     * $flag 0 = rules  1 = msg
     */
    public static function editRulesWithMsg($flag)
    {
        $rules = array(
            "title" => "required|max:100",
            'content' => 'required',
            'cid' => 'required|numeric',
        );

        $msg = array(
            "cid.required" => "文章必须选择分类！"
        );

        if ($flag == 0) {
            return $rules;
        } else {
            return $msg;
        }
    }

    //数据逻辑区===========================================================================

    /**
     * 保存或更新
     */
    public function saveData($classify, $title, $content, $img)
    {
        $this->title = $title;
        $this->cid = $classify->id;
        $this->time = time();
        $this->content = $content;
        $this->img = $img;

        return $this->save();
    }

}